﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace Soft.Net
{

    /// <summary>
    /// UDP广播基类
    /// </summary>
    public abstract class BaseUDPBroadcast : ErrorResponse, IDisposable
    {
        ~BaseUDPBroadcast()
        {
            this.Dispose();
        }

   
        /// <summary>
        /// 广播是否停止
        /// </summary>
        public bool Terminated
        {
            get
            {
                return this.mTerminated;
            }
            set
            {
                if (!this.mTerminated && value)
                {
                    this.Stop();
                }
            }
        }


        /// <summary>
        /// 服务的状态
        /// </summary>
        public ServerState State { get; protected set; } = ServerState.Stopped;


        /// <summary>
        /// 端口号
        /// </summary>
        public int Port { get; protected set; }


        /// <summary>
        /// UdpSocket
        /// </summary>
        protected Socket UDPSocket { get; set; }


        /// <summary>
        /// IP端点
        /// </summary>
        protected IPEndPoint IPEndPoint { get; set; }

      

        /// <summary>
        /// 最大的缓冲区大小
        /// </summary>
        public ushort MaxBufferSize
        {
            get
            {
                return this.mMaxBufferSize;
            }
            set
            {
                if (value < 32)
                {
                    value = 32;
                }
                this.mMaxBufferSize = value;
            }
        }


        /// <summary>
        /// 释放资源
        /// </summary>
        public virtual void Dispose()
        {
            try
            {
                this.Stop();
                if (this.UDPSocket != null)
                {
                    this.UDPSocket.Close();
                    this.UDPSocket.Dispose();
                    this.UDPSocket = null;
                }
            }
            catch
            {
            }
        }


        /// <summary>
        /// 开始启动
        /// </summary>
        /// <returns></returns>
        public virtual Result Start()
        {
            this.mTerminated = (this.State != ServerState.Started);
            return new Result();
        }


        /// <summary>
        /// 停止
        /// </summary>
        public virtual void Stop()
        {
            this.mTerminated = true;
            if (this.State == ServerState.Started)
            {
                if (this.mSocketAsyncEventArgs != null)
                {
                    this.mSocketAsyncEventArgs.Dispose();
                    this.mSocketAsyncEventArgs = null;
                }
                if (this.UDPSocket != null)
                {
                    this.UDPSocket.Shutdown(SocketShutdown.Both);
                    this.UDPSocket = null;
                }
                this.State = ServerState.Stopped;
            }
        }


        private bool mTerminated = true;


        private ushort mMaxBufferSize = 1024;


        protected byte[] mBuffer;

 

        /// <summary>
        /// Socket网络事件通知
        /// </summary>
        protected SocketAsyncEventArgs mSocketAsyncEventArgs;


    }
}
